package com.hankyung.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.hankyung.dto.CustUserDto;

public class CustDao extends DaoManager implements ICustUser {

	@Override
	public Connection getConnection() throws SQLException {
		Connection conn=null;
		String url="jdbc:oracle:thin:@localhost:1521:xe";
		String user="hr";
		String pass="hr";
		return conn=DriverManager.getConnection(url,user,pass);
	}
//-----------------------------------------------------

	@Override
	public ArrayList<CustUserDto> getAllCustList() {
		ArrayList<CustUserDto> list=new ArrayList<CustUserDto>();
		Connection conn=null;
		PreparedStatement psmt=null;
		ResultSet rs=null;
		String sql=" SELECT ID, NAME, ADDRESS FROM CUSTUSER " +
				" ORDER BY ID ASC";
		try {
			conn=this.getConnection();
			log("2/6 S");
			psmt=conn.prepareStatement(sql);
			log("3/6 S");
			rs=psmt.executeQuery();
			log("4/6 S");
			while(rs.next()){
				int i=1;
				CustUserDto dto=new CustUserDto();
				dto.setId(rs.getString(i++));
				dto.setName(rs.getString(i++));
				dto.setAddress(rs.getString(i++));
				list.add(dto);//
			}
			log("5/6 S");
		} catch (SQLException e) {
			log("getAllCustList: ",e);
		}finally{
			this.close(conn, psmt, rs);
			//log("6/6 S");
		}
		return list;
	}//

	@Override
	public boolean addCustUser(CustUserDto dto) {
		Connection conn=null;
		PreparedStatement psmt=null;
		int count=0;
		String sql=" INSERT  INTO CUSTUSER (ID, NAME, ADDRESS) " +
				     " VALUES (?,?,?)  ";
		try {
			conn=this.getConnection();
			log("2/6 S");
			int i=1;
			psmt=conn.prepareStatement(sql);
			psmt.setString(i++, dto.getId());
			psmt.setString(i++, dto.getName());
			psmt.setString(i++, dto.getAddress());
			log("3/6 S");
			count=psmt.executeUpdate();
			log("4/6 S");
			
		} catch (SQLException e) {
			log("addCustUser: ",e);
		}finally{
			this.close(conn, psmt, null);
		}
		
		return count>0?true:false;
	}
	
	
	
	
	
	
}
